package com.yunyao.dao.service.bamu.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.dao.mapper.BamuSysDeptMapper;
import com.yunyao.dao.model.BamuReconciliation;
import com.yunyao.dao.model.BamuSysDept;
import com.yunyao.dao.model.custom.DeptChildCountDAO;
import com.yunyao.dao.service.bamu.IBamuSysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 机构表 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service
public class BamuSysDeptServiceImpl extends ServiceImpl<BamuSysDeptMapper, BamuSysDept> implements IBamuSysDeptService {

    /**
     * 根据部门ID列表查询每个部门下的子部门数量
     *
     * @param idList 部门ID列表
     * @return 包含每个部门及其子部门数量的列表
     */
    @Override
    public List<DeptChildCountDAO> childCountByIds(List<String> idList) {
        return baseMapper.childCountByIds(idList);
    }

    @Override
    public void deleteById(String id) {
        LambdaUpdateWrapper<BamuSysDept> updateWrapper = new LambdaUpdateWrapper<BamuSysDept>()
                .eq(BamuSysDept::getId, id)
                .set(BamuSysDept::getIsDeleted, DataDeletedStatus.YES.getType());
        update(updateWrapper);
    }

    @Override
    public BamuSysDept getByNameDept(String name) {
        LambdaQueryWrapper<BamuSysDept> wrapper = new LambdaQueryWrapper<BamuSysDept>()
                .eq(BamuSysDept::getDeptName, name)
                .eq(BamuSysDept::getTenantId, "000000")
                .eq(BamuSysDept::getOrgType, "部门")
                .eq(BamuSysDept::getIsDeleted, DataDeletedStatus.NO.getType());
        return getOne(wrapper);
    }

    @Override
    public BamuSysDept getByNameOrg(String name) {
        LambdaQueryWrapper<BamuSysDept> wrapper = new LambdaQueryWrapper<BamuSysDept>()
                .eq(BamuSysDept::getDeptName, name)
                .eq(BamuSysDept::getTenantId, "000000")
                .eq(BamuSysDept::getOrgType, "组织")
                .eq(BamuSysDept::getIsDeleted, DataDeletedStatus.NO.getType());
        return getOne(wrapper);
    }

    @Override
    public BamuSysDept getById(String id) {
        // 验证ID有效性
        if (StringUtils.isBlank(id)) {
            return null;
        }
        // 构建查询条件，寻找ID为指定值且未被删除的采购合同记录
        LambdaQueryWrapper<BamuSysDept> wrapper =
                new LambdaQueryWrapper<BamuSysDept>()
                        .eq(BamuSysDept::getId, id)
                        .eq(BamuSysDept::getIsDeleted, DataDeletedStatus.NO.getType());
        // 执行查询并返回结果
        return getOne(wrapper);
    }


}
